/*
 * @Author: chunlai
 * @Date: 2023-07-25 09:48:14
 * @LastEditTime: 2025-08-19 16:12:57
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\translate\risks\rate.js
 * @Description: 试算接口数据组合
 */
import { countBirthBill } from "@/methods/date.js";
import { getDataFromUrl } from "@/utils/";
import { getCacheValue } from "@/methods/cache.js";
export const GetToRateparams = (rootState, getters) => {
  const insertStore = JSON.parse(JSON.stringify(rootState.insert)),
    pageCfg = rootState.page;
  const arr2 = getters["insert/bbrMultList"];
  const isNetInsure = getters["page/isNetInsure"];
  let risks = rootState.risks;
  let list = risks.filter((item) => item.ismain || item.checked);
  // 多被保人处理
  let bbrList = [];
  const len = arr2.length;
  for (let b = 0; b < len; b++) {
    const item = arr2[b];
    let obj = {
      sex: item.sex || "1",
      age: item.age * 1 || 0,
      risks: GetToRateData(list, b),
      socialInsuFlag: item.socialInsuFlag,
      occupationClass: item.occupationClass || "1", // 职业等级
    };
    // 生日单，年龄计算
    if (pageCfg.birthyBillAge !== 0) {
      const isBirthyBill = countBirthBill(item.birthDate);
      if (isBirthyBill) {
        obj.age += pageCfg.birthyBillAge;
      }
    }
    if (isNetInsure == "3") {
      obj.idNum = item.idNum;
      obj.birthDate = item.birthDate;
    }
    bbrList.push(obj);
  }
  // console.log("GetToRateparams---", bbrItem);
  let tbrInfo = JSON.parse(JSON.stringify(insertStore.applicant));
  // 生日单---投保人，年龄计算
  if (pageCfg.birthyBillAge !== 0) {
    const isBirthyBill = countBirthBill(tbrInfo.birthDate);
    if (isBirthyBill) {
      tbrInfo.age += pageCfg.birthyBillAge;
    }
  }
  let data = {
    appnt: {
      sex: tbrInfo.sex,
      age: tbrInfo.age,
    },
    insureds: bbrList,
  };
  if (isNetInsure == "3") {
    data.appnt.idNum = tbrInfo.idNum;
    data.appnt.birthDate = tbrInfo.birthDate;
  }

  return data;
};

export const GetToRateData = (list, index = 0) => {
  const gysName = getDataFromUrl("company");
  if (gysName == "ansheng") {
    return setApi2RateData(list, index);
  } else if (["taikang"].includes(gysName)) {
    return setApi3RateData(list, index);
  } else {
    return setApiRateData(list, index);
  }
};

export const setApiRateData = (list, index = 0) => {
  let data = list.map((item) => {
    let resData = {
      mainRiskCode: getCacheValue("insert", "mainRiskCode"), //险种编码
      riskCode: item.varietyCode, //险种编码
      riskName: item.productName,
      amnt: item.amnt, //保额
      prem: item.prem, //保费
      mult: item.mult || 1, //份数
      baseMoney: item.baseMoney, // 计算基数
      insureYear: item.insuYear, //保险期间
      insureYearType: item.insuYearType,
      insureDuration: item.payendyear == "趸交" ? "1000" : item.payendyear, //缴费年期
      insureDurationType: item.payendyearType,
      planCode: item.planCode || "",
      riskType: `${item.ismain ? "0" : "1"}`,
      payLimit: `${item.payendyear == "趸交" ? "0" : "12"}`, //缴费频次
    };
    /* ==== 码表统一查询赋值 ==== */
    // 3-2.基础+可选责任
    if (item.dutyList && item.dutyList.length > 0) {
      let dutyArr = [],
        dutyCode = "";
      // 用户选择则的值
      dutyArr = item.dutyList.filter((i) => i.isCheck).map((j) => j.value);
      // 固定值(baseDutyCode) + 用户选择则的值(数组转字符串) = jibenTrans中的key键值
      let mustDC = item.baseDutyCode || "1";
      if (dutyArr.includes("1")) {
        mustDC = "";
      }
      const transKey = mustDC + dutyArr.join("");
      /**转换基本责任代码*/
      if (item.jibenTrans) {
        for (let key in item.jibenTrans) {
          if (key == transKey) {
            dutyCode = item.jibenTrans[key];
          }
        }
      }
      resData.planCode = dutyCode;
    }
    // 可选责任列表 --- 类型2(list列表形式)
    if (item.liabilityList && item.liabilityList.length > 0) {
      let list2 = [],
        list3 = [];
      if (item.liabilityChooseType === "1") {
        // 长城
        list3 = item.liabilityList.filter((dut) => dut.amnt && dut.isChecked);
        list2 = list3.map((dut) => {
          let obj = {
            dutyCode: dut.dutyCode,
            dutyAmnt: dut.amnt,
            dutyPrem: dut.prem || "",
            dutyInsureYear: "",
            dutyInsureYearFlag: "",
          };
          return obj;
        });
      }
      resData.dutys = [...list2];
    }
    if (item.duty3List && item.duty3List.length > 0) {
      let listf2 = [],
        listf3 = [];
      listf3 = item.duty3List.filter((dut) => dut.isChecked);
      listf2 = listf3.map((dut) => {
        let obj = {
          dutyCode: dut.value,
          dutyAmnt: dut.amnt || "",
          dutyPrem: dut.prem || "",
        };
        return obj;
      });
      resData.dutys = [...listf2];
    }
    // 复星联合110176
    if (item.plansList) {
      const plan2Obj = item.plansList.find(
        (p) => p.mustChoose === "1" && p.mandatoryConditionsKey === "bbrMaxNum"
      );
      if (plan2Obj && index >= plan2Obj.mandatoryConditionsValue - 1) {
        resData.planCode = plan2Obj.value;
      }
    }
    /* ==== 非必传 ==== */
    // 1.年金险---领取年龄
    if (item.pullAge) {
      resData.getYear = item.pullAge;
      resData.getYearFlag = item.getYearFlag || "A";
    }
    // 1-2.年金险---领取频次
    if (item.pullMethod) {
      resData.getIntv = item.pullMethod;
    }
    // 1-3.年金险---领取方式
    if (item.getMode) {
      resData.getMode = item.getMode;
    }
    // 2.年金险---领取年龄
    if (item.renewFlag) {
      resData.renewFlag = item.renewFlag;
    }
    if (item.planPushDtys && item.planPushDtys == "1") {
      const isHas = resData.dutys.find(
        (ele) => ele.dutyCode == resData.planCode
      );
      if (!isHas) {
        let planObj = item.planDutyObj || {};
        let obj2 = {
          dutyCode: resData.planCode,
          dutyAmnt: planObj.dutyAmnt || "",
          dutyPrem: planObj.dutyPrem || "",
        };
        resData.dutys.push(obj2);
      }
    }
    // console.log('GetToRateData---',resData.planCode)
    return resData;
  });

  return data;
};
// 安盛isNewUiConfig
export const setApi2RateData = (list, index = 0) => {
  let riskArr = [];
  const zhuRisk = JSON.parse(JSON.stringify(list[0])),
    bbrItem = getCacheValue("insert", "bbrMultList")[index];
  // console.log("setApi2RateData", zhuRisk);
  const netBaseYz = {
    insureDuration: "1",
    insureDurationType: "Y",
    insureYear: "1",
    insureYearType: "Y",
    socialSecFlag: bbrItem.socialInsuFlag || "Y",
  };
  if (zhuRisk.isNewUiConfig) {
    const apiKeys = zhuRisk.apiKeyFields;
    Object.keys(apiKeys).map((key) => {
      if (
        !["planBigId", "social", "mjztemp"].includes(key) &&
        apiKeys[key] &&
        apiKeys[key] != "no"
      ) {
        const arr = apiKeys[key].split("-"),
          len = arr.length;
        for (let j = 0; j < len; j++) {
          const obj = {
            ...netBaseYz,
            riskCode: arr[j],
            planCode: zhuRisk.planVal,
          };
          riskArr.push(obj);
        }
      }
    });
  } else {
    riskArr = [
      {
        ...netBaseYz,
        riskCode: zhuRisk.planCode || zhuRisk.varietyCode || "",
        planCode: zhuRisk.planVal,
      },
    ];
    if (zhuRisk.apiCodes) {
      const duty2Code = zhuRisk.apiCodes.join("-").split("-");
      for (var i = 0; i < duty2Code.length; i++) {
        const temObj2 = {
          ...netBaseYz,
          riskCode: duty2Code[i], // 险别编码
          planCode: zhuRisk.planVal,
        };
        riskArr.push(temObj2);
      }
    }
    // 保障期间选择
    if (zhuRisk.insuYearStart) {
      riskArr[0].insuYearStart = zhuRisk.insuYearStart;
    }
    if (zhuRisk.insuYearEnd) {
      riskArr[0].insuYearEnd = zhuRisk.insuYearEnd;
    }
  }
  const payInfo = getCacheValue("insert", "payInfo");
  riskArr[0].prem = payInfo.totalPrem || "";
  return riskArr;
};

export const setApi3RateData = (list, index = 0) => {
  const zhuRisk = JSON.parse(JSON.stringify(list[0])),
    bbrItem = getCacheValue("insert", "bbrMultList")[index];
  const netBaseYz = {
    insureDuration: "1",
    insureDurationType: "Y",
    insureYear: "1",
    insureYearType: "Y",
  };
  let riskArr = [
    {
      ...netBaseYz,
      prem: zhuRisk.prem,
      amnt: zhuRisk.amnt,
      riskCode: zhuRisk.varietyCode,
    },
  ];

  // 1.泰康
  if (zhuRisk.dutyAmntPremTable && zhuRisk.dutyAmntPremTable.length > 0) {
    let tk1Arr = [];
    const len = zhuRisk.dutyAmntPremTable.length;
    for (let i = 0; i < len; i++) {
      const dty = zhuRisk.dutyAmntPremTable[i];
      let tkObj = {
        ...netBaseYz,
        prem: `${dty.baofei}`,
        amnt: `${dty.baoe}`,
        riskCode: dty.riskCode,
      };
      tk1Arr.push(tkObj);
    }
    riskArr = [...riskArr, ...tk1Arr];
  }
  const payInfo = getCacheValue("insert", "payInfo");
  riskArr[0].prem = payInfo.totalPrem || "";
  return riskArr;
};
